// import { areaList } from '@vant/area-data';
import {
  areaList
} from '../../../utils/areaData';
import {
  getUserInfo
} from "../../../utils/http";
import {
  sign,
  QueryDate
} from "../../../api/sign"
import {
  querystorelist
} from "../../../api/pm"

const {
  eventBus
} = getApp().globalData;
const app = getApp();
const globalData = app.globalData;
const {
  config,
  colorOption,
  windowInfo,
  curDept
} = app.globalData;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isData: false,
    pageNum: 1,
    userInfo: [],
    credit: 0,
    showData: {},
    outshow: false,

    dates: [
      // {value: 0, isPink: false},
      // {value: 29, isPink: false},
      // {value: 22, isPink: false},
      // {value: 23, isPink: false},
      // {value: 24, isPink: false},
      // {value: '+15', isPink: true},
      // {value: 26, isPink: false}
    ],
    // couponsList: [{
    //     "id": 1,
    //     "integral": 16,
    //     "goodsName": "满50-20券784547496546546546",
    //     "goodsPic": "http://niu.atcat.cn/mini-merchant/%E4%BC%98%E6%83%A0%E5%88%B8.png",
    //   },
    //   {
    //     "id": 1,
    //     "integral": 16,
    //     "goodsName": "满50-20券",
    //     "goodsPic": "http://niu.atcat.cn/mini-merchant/%E4%BC%98%E6%83%A0%E5%88%B8.png",
    //   },
    //   {
    //     "id": 999,
    //     "integral": 16,
    //     "goodsName": "满50-20券",
    //     "goodsPic": "http://niu.atcat.cn/mini-merchant/%E4%BC%98%E6%83%A0%E5%88%B8.png",
    //   },
    // ],
    couponsList: null
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(globalData.userInfo.phone)
    this.setData({
      phone: globalData.userInfo.phone
    })
    // console.log(11111)
    // console.log(config.localImgSrc)
    // 添加事件监听器
    eventBus.on("shortcutItem", this.handleShortcutItem);
    // this.setData({
    //   userInfo: globalData.userInfo
    // })
    getUserInfo(() => {
      this.setData({
        userInfo: globalData.userInfo
      })
    })
    this.loadDates();
    this.QueryDate();
    this.getstorelist();

  },
  qinter() {
    // this.setData({
    //   userInfo: globalData.userInfo
    // })
    getUserInfo(() => {
      this.setData({
        userInfo: globalData.userInfo
      })
    })
  },
  loadDates: function () {
    const currentDate = new Date();
    const newDates = []; // 用于存放新格式的日期对象

    // 循环七次，获取当前日期及前六天的日期，并转换为所需格式
    for (let i = 0; i < 7; i++) {
      const date = new Date(currentDate);
      date.setDate(currentDate.getDate() - i);
      const dayValue = date.getDate();

      // 根据你的需求定义isPink的逻辑，这里假设不是特别的日子则isPink为false
      const isSpecialDay = false; // 请根据实际情况定义哪些日子是特别的
      const formattedDateObj = {
        value: dayValue,
        isPink: isSpecialDay
      };

      newDates.push(formattedDateObj);
    }

    // 将新的日期对象数组与原数组合并
    this.setData({
      dates: [...this.data.dates, ...newDates.reverse()], // 反转并合并数组
    });
  },
  onUnload() {
    // 移除事件监听器
    eventBus.off("shortcutItem", this.handleShortcutItem);
  },

  handleShortcutItem(data) {
    this.setData({
      showData: data,
    });
  },
  goBack() {
    wx.navigateBack()
  },

  onShareAppMessage() {},
  //签到打卡按钮
  sign() {
    // wx.showToast({
    //   title: '签到成功',
    //   duration: 500,
    // })
    // console.log('签到成功')
    sign({
      callBack: (res) => {
        console.log(res)
        getUserInfo(() => {
          this.setData({
            userInfo: globalData.userInfo
          })
        })
        this.QueryDate();
        wx.showToast({
          title: '签到成功',
        })
      }

    })

  },
  QueryDate() {
    // console.log(333)
    const currentDate = new Date(); // 获取当前日期
    const formattedCurrentDate = this.formatDate(currentDate);
    const dateSevenDaysAgo = new Date(currentDate.getTime() - 7 * 24 * 60 * 60 * 1000); // 计算七天前的日期
    const formattedDateSevenDaysAgo = this.formatDate(dateSevenDaysAgo);

    QueryDate({
      data: {
        "startDate": formattedDateSevenDaysAgo,
        "endDate": formattedCurrentDate,

      },
      callBack: (res) => {
        let dates = this.data.dates
        console.log(res.data)
        let dataArray = res.data;
        dataArray.forEach(item => {
          // 提取日期中的日
          let day = new Date(item.createTime).getDate();

          // 在dates数组中查找匹配的日期
          let foundIndex = dates.findIndex(date => date.value === day);

          if (foundIndex !== -1) {
            // 如果找到，更新isPink和value
            dates[foundIndex].isPink = true;
            dates[foundIndex].value = '+' + item.integral;
          }
          // console.log(dates);
        });
        this.setData({
          dates: dates
        })


      }

    })
  },
  //修改时间格式
  formatDate: function (date) {
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的，所以需要+1
    const day = date.getDate().toString().padStart(2, '0');
    return `${year}-${month}-${day}`;
  },
  //获取商品列表
  getstorelist() {
    wx.showLoading({
      title: '加载中',
    });

    querystorelist({
      data: {
        "pageNum": this.data.pageNum,
        "pageSize": 4
      },
      callBack: (res) => {
        let oldData
        if (!this.data.couponsList) {
          oldData = []
        } else {
          oldData = this.data.couponsList
        }
        let newData = oldData.concat(res.rows.map(item => ({
          ...item, // 复制当前对象的所有属性
          // goodsPic: `config.localImgSrc${item.goodsPic}`, // 修改goodsPic属性，加上新的URL前缀
          goodsPic: config.localImgSrc.concat(item.goodsPic), // 修改goodsPic属性，加上新的URL前缀
        })))
        // console.log(newData)
        // console.log(res)
        // console.log(config.localImgSrc)
        // const url=config.localImgSrc
        // console.log(url)



        this.setData({
          couponsList: newData
        })
        if (this.data.couponsList.length == res.total || res.rows.length < 4) {
          this.setData({
            isData: true
          })
        }
        wx.stopPullDownRefresh();
        wx.hideLoading();
      }


    })
  },
  // 点击商品显示外部操作菜单
  navTostoredetail(e) {
    // console.log(e.currentTarget.dataset.item);
    this.setData({
      outshow: true,
      itemdetail: e.currentTarget.dataset.item
    })
    console.log(this.data.itemdetail)
  },

  onReachBottom() {

    if (this.data.isData) return;
    console.log('我触底了');
    this.data.pageNum = this.data.pageNum + 1;
    this.getstorelist();

  },






});